/*******************************************************************************
 * Copyright (c) 2011 Grupo de Sistemas Inteligentes (GSI) - DIT UPM
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package es.upm.dit.gsi.eclipse.jadex.adfmanager.wizards;


import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

/**
 * This page allows to get the following information in order to create a new reference to an existing capability
 *  - Capability name
 *  - Capability File
 *    
 *  @author Pablo Muñoz
 */

public class AddExpressionWizardPage extends WizardPage {
	private Combo typeCombo;
	private Text nameText;
	private Text classText;
	private Text valueText;
	private Combo languageCombo;
	private Button languageEnabled;
	private Combo exportedCombo;
	private Button exportedEnabled;
	private Text descriptionText;
	private Button descriptionEnabled;	

	/**
	 * Constructor for AgentWizardPage
	 * 
	 * @param pageName
	 */
	public AddExpressionWizardPage(ISelection selection) {
		super("wizardPage");
		setTitle("Expression");
		setDescription("This wizard adds a new Expression to the selected Agent");
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 4;
		layout.verticalSpacing = 9;
		PlatformUI.getWorkbench().getHelpSystem().setHelp(container, "es.upm.dit.gsi.eclipse.jadex.adfmanager.addExpressionWizard");
		
		Label label1 = new Label(container, SWT.NULL);
		label1.setText("&Type:");
		typeCombo = new Combo(container, SWT.READ_ONLY);
		typeCombo.setItems (new String [] {"expression","condition", "expressionref", "conditionref"});
		typeCombo.select(0);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 3;
		typeCombo.setLayoutData(gd);
		typeCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label label2 = new Label(container, SWT.NULL);
		label2.setText("&Name:");
		nameText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		nameText.setLayoutData(gd);
		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label label3 = new Label(container, SWT.NULL);
		label3.setText("&Class:");
		classText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		classText.setLayoutData(gd);
		classText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label label4 = new Label(container, SWT.NULL);
		label4.setText("&Value:");
		valueText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 3;
		valueText.setLayoutData(gd);
		valueText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label separator = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		separator.setLayoutData(gd);
		gd.horizontalSpan = 4;
		
		Label label5 = new Label(container, SWT.NULL);
		label5.setText("&Language (optional):");
		languageCombo = new Combo(container, SWT.SIMPLE);
		languageCombo.setItems (new String [] {"jcl"});
		languageCombo.select(0);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		languageCombo.setLayoutData(gd);
		languageCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		languageEnabled = new Button (container, SWT.CHECK);
		languageEnabled.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		
		Label label6 = new Label(container, SWT.NULL);
		label6.setText("&Exported (optional):");
		exportedCombo = new Combo(container, SWT.READ_ONLY);
		exportedCombo.setItems (new String [] {"true","false"});
		exportedCombo.select(1);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		exportedCombo.setLayoutData(gd);
		exportedCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		exportedEnabled = new Button (container, SWT.CHECK);
		exportedEnabled.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		
		Label label7 = new Label(container, SWT.NULL);
		label7.setText("&Description (optional):");
		descriptionText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		descriptionText.setLayoutData(gd);
		descriptionText.setLayoutData(gd);
		descriptionText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		descriptionEnabled = new Button (container, SWT.CHECK);
		descriptionEnabled.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});	
		
		initialize();
		dialogChanged();
		setControl(container);
	}

	/**
	 * Tests if the current workbench selection is a suitable container to use.
	 */
	private void initialize() {
		enabledPropertiesChanged();
	}

	/**
	 * Ensures that the all text fields are set correctly.
	 */
	private void dialogChanged() {

		if(nameText.getText().length() == 0){
			updateStatus("A name must be specified for the new expression");
			return;
		}
		if(classText.getText().length() == 0){
			updateStatus("A class must be specified for the new expression");
			return;
		}
		if(valueText.getText().length() == 0){
			updateStatus("A value must be specified for the new expression");
			return;
		}
		if(languageCombo.getText().length() == 0 && languageEnabled.getSelection()){
			updateStatus("A language must be specified if enabled");
			return;
		}
		if(descriptionText.getText().length() == 0 && descriptionEnabled.getSelection()){
			updateStatus("A description must be specified if enabled");
			return;
		}	
		updateStatus(null);
	}
	
	
	
	private void enabledPropertiesChanged(){
		languageCombo.setEnabled(languageEnabled.getSelection());
		exportedCombo.setEnabled(exportedEnabled.getSelection());
		descriptionText.setEnabled(descriptionEnabled.getSelection());
		dialogChanged();
	}

	/*
	 * This method is called in order to update the status message 
	 */
	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}

	/**
	 * @return the type of the expression
	 */
	public String getExpressionType() {
		return typeCombo.getText();
	}

	/**
	 * @return the name of the expression
	 */
	public String getExpressionName() {
		return nameText.getText();
	}

	/**
	 * @return the class of the expression
	 */
	public String getExpressionClass() {
		return classText.getText();
	}

	/**
	 * @return the value of the expression
	 */
	public String getExpressionValue() {
		return valueText.getText();
	}

	/**
	 * @return the language of the expression or null if the language option is disabled
	 */
	public String getExpressionLanguage() {
		if(languageEnabled.getSelection()){
			return languageCombo.getText();
		}
		return null;
	}

	/**
	 * @return the exported option value of the expression or null if the exported option is disabled
	 */
	public String getExpressionExported() {
		if(exportedEnabled.getSelection()){
			return exportedCombo.getText();
		}
		return null;
	}

	/**
	 * @return the description of the expression or null if the description option is disabled
	 */
	public String getExpressionDescription() {
		if(descriptionEnabled.getSelection()){
			return descriptionText.getText();
		}
		return null;
	}
	
	
}